class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode *> q;
        q.push(root);
        int result;
        while (!q.empty()) {
            int size = q.size();
            result = q.front()->val;
            for (int i = 0; i < size; ++i) {
                TreeNode *node = q.front();
                q.pop();
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
        }
        return result;
    }
};

class Solution2 {
private:
    void findLeftValue(TreeNode *node, int leftDepth, int &maxDepth, int &leftValue) {
        if (!node) return;
        if (!node->left && !node->right) {
            if (leftDepth > maxDepth) {
                maxDepth = leftDepth;
                leftValue = node->val;
            }
            return;
        }
        if (node->left) findLeftValue(node->left, leftDepth + 1, maxDepth, leftValue);
        if (node->right) findLeftValue(node->right, leftDepth + 1, maxDepth, leftValue);
    }

public:
    int findBottomLeftValue(TreeNode* root) {
        int leftDepth = INT_MIN;
        int leftValue = 0;
        findLeftValue(root, 0, leftDepth, leftValue);
        return leftValue;
    }
};